<?php
Class Coquanngoai extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'coquanngoai';
        $this->load->model('Mcoquanngoai');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 12;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mcoquanngoai->getCoquanngoaiDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
		//echo '<pre>';die(print_r($data));
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function add()
    {       
        $this->load->library('form_validation');
        $this->form_validation->set_rules('tencoquan', 'Tên cơ quan', 'required');
        $this->form_validation->set_rules('diachi', 'Địa chỉ', 'required');
       
        if($this->form_validation->run() === TRUE)
        {
            $tencoquan = $this->getParamString('tencoquan');
            $diachi = $this->getParamString('diachi');
            $run = $this->Mcoquanngoai->addCoquanngoaiDb($tencoquan,$diachi);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/coquanngoai/index";</script>');
            }
        }
        $this->data['action'] = 'add';
        $this->load->view('template/layout', $this->data);
        
    }
    
	public function delete(){
        $id = $this->getParamUri(4,1);
        $run = $this->Mcoquanngoai->deleteCoquanngoaiDB($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>alert("Xóa thành công");window.location="'.$this->data['baseurl'].'manage/coquanngoai/index";</script>');
        }
    }

    public function edit()
    {
		$id = $this->getParamUri(4,1);
		$this->data['data'] = $this->Mcoquanngoai->getCoquanngoaiDetail($id);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('tencoquan', 'Tên cơ quan', 'required');
        $this->form_validation->set_rules('diachi', 'Địa chỉ', 'required');
        if($this->form_validation->run() === TRUE)
        {
			$tencoquan = $this->getParamString('tencoquan');
			$diachi = $this->getParamString('diachi');

            $run = $this->Mcoquanngoai->editCoquanngoaiDb($tencoquan,$diachi,$id);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/coquanngoai/index";</script>');
            }
        }
        $this->data['action'] = 'edit';
        $this->load->view('template/layout', $this->data);
    }
    
    
    
}